﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FTchina.Library;
using PWMIS.DataMap.Entity;
using FTchina.Logic;

public partial class sso_BPMSSO : System.Web.UI.Page
{
    /// <summary>
    /// 用UID，时间，组合后 加密 ,key为ROBOTKEY补齐16位*
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (LoginLogic.RobotLogin())
        {//自动执行
            LogHelper.Info("BPM机器人验证成功");
            CheckUser();

        }
        else
        {
            Response.Write("验证失败，请检查");
        }

    }

    /// <summary>
    /// 检查用户
    /// </summary>
    protected void CheckUser()
    {
        string skey = RequestHelper.GetRequst("skey");//加密后的字符串
        string path = RequestHelper.GetRequst("path");//访问路径
        string key = System.Configuration.ConfigurationManager.AppSettings["ROBOTKEY"].ToString().PadRight(16, '*');//robotkey
        string dstr = SecurityHelper.Decrypt(skey, key);//解密字符串
        if (dstr.Contains(','))
        {//不含逗号说明是篡改

            string UID = dstr.Split(',')[0].Replace("BPM/", "");
            try
            {
                DateTime Date = Convert.ToDateTime(dstr.Split(',')[1].ToString());
                if (DateTime.Now.Subtract(Date).TotalMinutes < 10)
                {//超过10分钟，认为登录失败，考虑到服务器之间的时差
                    Response.Write("登录失败，请从流程内点击");
                }
                else
                {//认证登录
                    Login(UID, path);
                }
            }
            catch (FormatException x)
            {
                Response.Write("参数认证失败，请检查");
                LogHelper.Warn("参数篡改！" + path + RequestHelper.GetUserIp() + x.Message);
            }

        }
        else
        {
            Response.Write("认证失败，请检查");
            LogHelper.Warn("参数篡改！" + path + RequestHelper.GetUserIp());
        }
    }

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="UID"></param>
    /// <param name="path"></param>
    protected void Login(string UID, string path)
    {
        FTchina.Models.Master mMaster = new FTchina.Models.Master();
        FTchina.Models.UserList mUserList = new FTchina.Models.UserList();

        OQLCompare cmp = new OQLCompare(mMaster);
        OQLCompare cmpResult = (cmp.Comparer(mMaster.Nicheng, OQLCompare.CompareType.Equal, UID));


        OQL q = new OQL(mMaster);
        q.Select(mMaster.Usergroup, mMaster.Nicheng, mMaster.State, mMaster.Master_Password, mMaster.Link).Where(cmpResult);
        q.Join(mUserList).On(mMaster.Link, mUserList.Id);



        List<FTchina.Models.Master> masterlist = EntityQuery<FTchina.Models.Master>.QueryList(q, mMaster);

        if (masterlist.Count == 0)
        {

            FTchina.Library.LogHelper.Warn("用户:" + UID + " BPM登陆失败IP地址:" + RequestHelper.GetUserIp());
            Response.Write("请联系系统管理员注册" + UID + "<br/>魏宝辉8088613");
            // Alert.ShowInParent("请联系系统管理员注册");

        }
        else
        {
            foreach (FTchina.Models.Master m in masterlist)
            {

                if (m.State == "1")
                {//账户状态正常


                    mUserList.Id = Convert.ToInt32(m.Link);//获取指定ID的UserList
                    EntityQuery<FTchina.Models.UserList> qUserlist = new EntityQuery<FTchina.Models.UserList>(mUserList);
                    qUserlist.FillEntity(mUserList);


                    MasterHelper.ParentItemName = UserListLogic.getParentItemNameByParentID(mUserList.ParentID);

                    MasterHelper.UName = mUserList.UName;//用户组织架构的姓名
                    //LogHelper.Warn("MasterHelper.UName" + MasterHelper.UName);
                    MasterHelper.UID = mUserList.UID;
                    MasterHelper.MasterID = SecurityHelper.EncryptMD5(FTchina.basic.GetGuid());
                    MasterHelper.UserGroup = m.Usergroup;
                    MasterHelper.UserGroupName = LoginLogic.GetUserGroupNameByID(m.Usergroup);
                    MasterHelper.Nickname = m.Nicheng;
                    MasterHelper.Luruid = m.Link;
                    MasterHelper.LoginIP = RequestHelper.GetUserIp();
                    string Area_Name = string.Empty;
                    MasterHelper.BssAreaCode = LoginLogic.GetBssAreaCodeByName(MasterHelper.ParentItemName, out Area_Name);
                    MasterHelper.EssAreaName = Area_Name;

                    FTchina.Library.LogHelper.Info("BPM用户:" + UID + MasterHelper.LoginIP + "登陆成功");
                    #region 插入登录日志
                    FTchina.Models.Login_Log mLogin_Log = new FTchina.Models.Login_Log();
                    mLogin_Log.Id = 0;
                    mLogin_Log.UName = mUserList.UName;
                    mLogin_Log.UID = mUserList.UID;
                    mLogin_Log.GroupName = MasterHelper.UserGroupName;
                    mLogin_Log.Ip = RequestHelper.GetUserIp();
                    mLogin_Log.LoginDate = DateTime.Now;
                    FTchina.BLL.Login_LogManager.AddOrUpdateLogin_Log(mLogin_Log);
                    #endregion
                    Response.Redirect(path);
                    ///AlertLogic.ShowResultAndRedirect("登录成功", "恭喜", "/default.aspx", true);
                }
                else
                {
                    Response.Write("您的帐户已被锁定，请与系统管理员联系！" + UID + "<br/>魏宝辉8088613");
                }
            }
        }
    }
}